package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Institution;
import com.ebring.shareEducation.bean.InstitutionCode;
import com.ebring.shareEducation.bean.admin.InstitutionAll;
import com.ebring.shareEducation.dao.InstitutionAllMapper;
import com.ebring.shareEducation.dao.InstitutionCodeMpper;
import com.ebring.shareEducation.dao.InstitutionMapper;
import com.ebring.shareEducation.service.InstitutionService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class InstitutionServiceImpl implements InstitutionService {

	@Autowired
	private InstitutionMapper institutionMapper;

	@Autowired
	private InstitutionCodeMpper institutionCodeMapper;

	@Autowired
	private InstitutionAllMapper institutionAllMapper;

	/**
	 * 添加一级机构和下级机构
	 *
	 * @return
	 */

	@Override
	public RspMsg<Institution> insertInstitution(Institution institution) {
		RspMsg rspMsg = new RspMsg();
		String institutionId = institution.getParentId();
		/*String level = "2";// 前台传的级别*/
		/*int in = Integer.parseInt(level);// 3*/
		// 查询下级机构的数量
		if (institutionId != null) {
			Institution key = institutionMapper.selectByPrimaryKey(institutionId);
			/*String parentId = key.getInstitutionId()+1;*/
			String parentId = key.getInstitutionId();
			String name = key.getInstitutionName();
			String string2 = key.getInstitutionLevel();// 后台查的级别
			int j = Integer.parseInt(string2);// 1
			Integer ji = j + 1;
			String institutionLevel = ji.toString();
			Integer count = institutionMapper.selectLevelCount(institutionLevel);
//
			/*if (in < j || in > ji) {
				rspMsg.setCode(0);
				rspMsg.setMsg("只能选择下级或平级");
				return rspMsg;
			}*/
			int y = 2;
			String substring = parentId.substring(0, y);
			int parseInt = Integer.parseInt(substring);
			Integer inte = parseInt + 10;
			String str = inte.toString();
			/*if (count >= 9) {
				Integer countis = count + 1;
				String string = countis.toString();
				String concat = str.concat(string);
				institution.setParentId(parentId);
				institution.setParentName(name);
				institution.setInstitutionId(concat);
				int i = institutionMapper.insertSelective(institution);
				if (i == 0) {
					rspMsg.setCode(0);
					rspMsg.setMsg("添加机构失败");
					return rspMsg;
				}
				rspMsg.setOK();
				return rspMsg;
			} else {*/
			Integer countis = count + 1;
			String id = str + "0" + countis;
			institution.setParentId(parentId);
			institution.setParentName(name);
			institution.setInstitutionId(id);
			institution.setInstitutionLevel(institutionLevel);
			int i = institutionMapper.insertSelective(institution);
			if (i == 0) {
				rspMsg.setCode(0);
				rspMsg.setMsg("添加机构失败");
				return rspMsg;
			}
			rspMsg.setOK();
			return rspMsg;
		}
		/*}*/
		return rspMsg;
	}

	/**
	 * 添加平级机构
	 *
	 * @param institution
	 * @return
	 */
	public RspMsg<Institution> insertSameInstitution(Institution institution) {
		RspMsg rspMsg = new RspMsg();
		String institutionId = institution.getInstitutionId();
		String level = institution.getInstitutionLevel();
		int y = 2;
		String str = institutionId.substring(0, y);
		// 查询level级别机构的数量
		Integer count = institutionMapper.selectLevelCount(level);
		// 查询上级机构
		InstitutionAll institutionAll = this.getParent(institutionId).getResult();
		if (institutionAll == null) {
			institution.setParentId("0");
			institution.setParentName("0");
		} else {
			institution.setParentId(institutionAll.getParentId());
			institution.setParentName(institutionAll.getParentName());
		}
		if (count >= 9) {
			Integer countis = count + 1;
			String string = countis.toString();
			String concat = str.concat(string);
			institution.setInstitutionId(concat);
			int i = institutionMapper.insertSelective(institution);
			if (i == 0) {
				rspMsg.setCode(0);
				rspMsg.setMsg("添加平级机构失败");
				return rspMsg;
			}
			rspMsg.setOK();
			return rspMsg;
		} else {
			Integer countis = count + 1;
			String id = str + "0" + countis;
			institution.setInstitutionId(id);
			int i = institutionMapper.insertSelective(institution);
			if (i == 0) {
				rspMsg.setCode(0);
				rspMsg.setMsg("添加平级机构失败");
				return rspMsg;
			}
			rspMsg.setOK();
			return rspMsg;
		}
	}

	/**
	 * 删除机构
	 */
	public RspMsg<Institution> deleteInstitution(String[] institutionId) {
		RspMsg rspMsg = new RspMsg();
		int sum = 0;
		for (String s : institutionId) {
			institutionMapper.deleteByPrimaryKey(s);
			sum++;
		}
		if (sum > 0) {
			rspMsg.setOK();
			rspMsg.setMsg("删除成功");
			rspMsg.setResult(sum);
			return rspMsg;
		}
		rspMsg.setCode(0);
		rspMsg.setMsg("删除失败");
		return rspMsg;
	}

	/**
	 * 修改机构
	 */
	@Override
	public RspMsg<Institution> updateInstitution(Institution institution) {
		RspMsg rspMsg = new RspMsg();
		int i = institutionMapper.updateByPrimaryKeySelective(institution);
		if (i == 0) {
			rspMsg.setCode(0);
			rspMsg.setMsg("修改失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;
	}

	/**
	 * 模糊查询机构列表
	 */
	@Override
	public RspMsg<List<InstitutionAll>> select(String institutionId, String institutionName,String parentId,String institutionlevel) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
	/*	String str1=null;
		String str2=null;
		if(null != institutionId) {
			 str1="%"+institutionId+"%";
		}
	    if(null != institutionName) {
	    	 str2="%"+institutionName+"%";
	    }*/
		List<InstitutionAll> institutionList = institutionAllMapper.selectLike(institutionId, institutionName,parentId,institutionlevel);
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关机构");
			return rspMsg;
		}
		PageInfo<InstitutionAll> pageInfo = new PageInfo<>(institutionList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("institutionList", institutionList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	/**
	 * 根据用户机构id查询机构
	 */
	public RspMsg<List<InstitutionAll>> selectAll(String institutionId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		// 查询所有一级机构
		List<InstitutionAll> institutionList = institutionAllMapper.selectLevelAll(institutionId);
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("未找到数据");
			return rspMsg;
		}
		PageInfo<InstitutionAll> pageInfo = new PageInfo<>(institutionList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("institutionList", institutionList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	/**
	 * 查询下级机构
	 */
	/*@Override
	public RspMsg<Map> getLower(String instlevel) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<InstitutionAll> institutionList = institutionAllMapper.selectGetLower(instlevel);
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关下级");
			return rspMsg;
		}
		PageInfo<InstitutionAll> pageInfo = new PageInfo<>(institutionList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("institutionList", institutionList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}*/

	/**
	 * 查询下级机构
	 */
	@Override
	public RspMsg<Map> getLower(String institutionId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<InstitutionAll> institutionList = institutionAllMapper.selectGetLower(institutionId);
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关下级");
			return rspMsg;
		}
	/*	PageInfo<InstitutionAll> pageInfo = new PageInfo<>(institutionList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());*/
		map.put("institutionList", institutionList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	@Override
	public RspMsg<Map> getLower1(String institutionId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<InstitutionAll> institutionList = institutionAllMapper.selectGetLower1(institutionId);
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关下级");
			return rspMsg;
		}
		PageInfo<InstitutionAll> pageInfo = new PageInfo<>(institutionList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("institutionList", institutionList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	/**
	 * 查询上级机构
	 */
	@Override
	public RspMsg<InstitutionAll> getParent(String institutionId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		Institution institution = institutionMapper.selectByPrimaryKey(institutionId);
		String parentId = institution.getParentId();
		if (StringUtils.isEmpty(parentId)) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关上级");
			return rspMsg;
		}
		InstitutionAll institutionList = institutionAllMapper.selectgetParent(parentId);
		if (StringUtils.isEmpty(institutionList)) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有找到相关上级");
			return rspMsg;
		}

		rspMsg.setOK();
		rspMsg.setResult(institutionList);
		return rspMsg;
	}



	/**
	 * 查询总行机构
	 *
	 * @return
	 */
	@Override
	public RspMsg<List<Institution>> selectOne() {
		RspMsg rspMsg = new RspMsg();
		List<Institution> institutionList = institutionMapper.selectInstitution();
		if (institutionList.isEmpty() || institutionList == null) {
			rspMsg.setCode(0);
			rspMsg.setMsg("未查到相关数据");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setResult(institutionList);
		return rspMsg;
	}

	/**
	 * 查询所有机构级别
	 */
	@Override
	public RspMsg<List<InstitutionCode>> insertInstitutionCode(String parentId) {
		RspMsg rspMsg = new RspMsg();
		List list = new ArrayList();
		Institution selectByPKey = this.institutionMapper.selectByPKey(parentId);
		if (StringUtils.isEmpty(selectByPKey)) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有相关数据");
			return rspMsg;
		}
		String institutionLevel = selectByPKey.getInstitutionLevel();
		int parseInt = Integer.parseInt(institutionLevel);
		Integer Level = parseInt + 1;
		String string = Level.toString();
		InstitutionCode selectAll = this.institutionCodeMapper.selectLevel(string);
		/*InstitutionCode select = this.institutionCodeMapper.selectLevel(string);*/
		if (StringUtils.isEmpty(selectAll)) {
			rspMsg.setCode(0);
			rspMsg.setMsg("没有相关数据");
			return rspMsg;
		}
		list.add(selectAll);
		/*list.add(select);*/
		rspMsg.setOK();
		rspMsg.setResult(list);
		return rspMsg;
	}
}
